package com.feiwo.nonego.rpc.proxy.cglib;

import com.feiwo.nonego.rpc.proxy.api.BaseProxyFactory;
import com.feiwo.nonego.rpc.proxy.api.ProxyFactory;
import com.feiwo.nonego.rpc.spi.annotation.SPIClass;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

/**
 *
 *
 * @author feiwo 2025-08-14
 */
@SPIClass
public class CglibProxyFactory<T> extends BaseProxyFactory<T> implements ProxyFactory {

    private final Enhancer enhancer = new Enhancer();

    @SuppressWarnings("unchecked")
    @Override
    public <T> T getProxy(Class<T> clazz) {
        enhancer.setInterfaces(new Class[]{clazz});
        enhancer.setCallback((InvocationHandler) (o, method, objects) -> objectProxy.invoke(o, method, objects));
        return (T) enhancer.create();
    }
}
